﻿using System.Collections.Generic;
using LBoL.Base;
using LBoL.ConfigData;
using LBoL.Core;
using LBoL.Core.Battle;
using LBoL.Core.Battle.BattleActions;
using LBoL.Core.Battle.Interactions;
using LBoL.EntityLib.Cards.Character.Cirno;
using LBoL.EntityLib.StatusEffects.Others;
using LBoLEntitySideloader.Attributes;
using ProbeMod.Cards.Template;
using LBoL.Core.Cards;
using System.Linq;
using LBoL.Base.Extensions;
using LBoL.Core.Randoms;

namespace ProbeMod.Cards
{
    /// <summary>
    /// 枢纽
    /// </summary>
    public sealed class ProbeNexusDef : ProbeCardTemplate
    {
        public override CardConfig MakeConfig()
        {
            CardConfig config = GetCardDefaultConfig();

            config.Rarity = Rarity.Rare;
            config.Type = CardType.Friend;
            config.TargetType = TargetType.Nobody;
            config.Colors = new List<ManaColor>() { ManaColor.Blue,ManaColor.White };

            config.Cost = new ManaGroup() { Any = 1, Blue = 3 ,White = 1};
            config.UpgradedCost = new ManaGroup() { Any = 1, Blue = 1, White = 1 };

            config.Loyalty = 5;
            config.UpgradedLoyalty = 5;

            //  被动耐久变化
            config.PassiveCost = 1;
            config.UpgradedPassiveCost = 1;

            //  主动耐久变化
            config.ActiveCost = -2;
            config.UpgradedActiveCost = -2;

            //  大招消耗耐久.
            config.UltimateCost = -8;
            config.UpgradedUltimateCost = -8;

            config.Value1 = 1;  //  被动耐久变化
            config.Value2 = 3;  //  主动找牌的数量
            

            config.RelativeEffects = new List<string>() { "ProbeBuildingKeywordSe" };
            config.UpgradedRelativeEffects = new List<string>() { "ProbeBuildingKeywordSe" };

            config.RelativeCards = new List<string>() { "ProbePhotonOverload", "ProbePylon", "ProbeShieldBattery", "ProbeBC" };
            config.UpgradedRelativeCards = new List<string>() { "ProbePhotonOverload", "ProbePylon", "ProbeShieldBattery", "ProbeBC" };

            config.Index = CardIndexGenerator.GetUniqueIndex(config);
            return config;
        }
    }

    [EntityLogic(typeof(ProbeNexusDef))]
    public sealed class ProbeNexus : ProbeFriendCard
    {
        /// <summary>
        /// 被动：+1 手牌随机一张同伴牌+1耐久
        /// </summary>
        /// <returns></returns>
        public override IEnumerable<BattleAction> GetPassiveActions()
        {
            //Triigger the effect only if the card has been summoned. 
            if (!base.Summoned || base.Battle.BattleShouldEnd)
            {
                yield break;
            }
            base.NotifyActivating();
            //Increase base loyalty.
            base.Loyalty += base.PassiveCost;
            var handFriend = Battle.HandZone.Where((Card card) => card.CardType == CardType.Friend && card != this).Sample(BattleRng);

            if (handFriend != null)
            {
                handFriend.Loyalty += base.Value1;
            }

            yield break;
        }

        /// <summary>
        /// 主动 -2 从随机三张普通探机建筑牌中选择一张置入手牌
        /// 终结 -8 将水晶塔/+、光子炮台/+、护盾电池/+和光子过载/+置入手牌
        /// </summary>
        /// <param name="selector"></param>
        /// <param name="consumingMana"></param>
        /// <param name="precondition"></param>
        /// <returns></returns>
        protected override IEnumerable<BattleAction> Actions(UnitSelector selector, ManaGroup consumingMana, Interaction precondition)
        {
            if (precondition == null || ((MiniSelectCardInteraction)precondition).SelectedCard.FriendToken == FriendToken.Active)
            {
                base.Loyalty += base.ActiveCost;

                Card[] array = base.Battle.RollCardsWithoutManaLimit(
                new CardWeightTable
                (
                    RarityWeightTable.OnlyCommon, //Rarity distribution of the cards. (BattleCard: 40% Common, 40% Uncommon, 20% Rare)
                    OwnerWeightTable.OnlyPlayer, //Player and Neutral card pool. (Valid: Includes the Character, Act 1 Boss Exhibit and Neutral cards.) 
                    CardTypeWeightTable.OnlyFriend, //Card types. (Can Be Loot: Can be Attack, Defense, Skill, ability; Cannot be Tools).
                    false
                ),
                base.Value2, //Total amount of card to choose from.
                (CardConfig config) => config.Id != base.Id && config.RelativeEffects.Contains("ProbeBuildingKeywordSe"));  //  有探机建筑标签

                MiniSelectCardInteraction interaction = new MiniSelectCardInteraction(array, false, false, false)
                {
                    Source = this
                };
                yield return new InteractionAction(interaction, false);

                Card selectedCard = interaction.SelectedCard;   
                yield return new AddCardsToHandAction(new Card[] { selectedCard });//  选择卡牌置入手牌

                yield break;
            }
            base.Loyalty += base.UltimateCost;
            base.UltimateUsed = true;

            //  置入4张牌
            var pylon = Library.CreateCard<ProbePylon>(IsUpgraded);
            var connon = Library.CreateCard<ProbeBC>(IsUpgraded);
            var ShieldBattery = Library.CreateCard<ProbeShieldBattery>(IsUpgraded);
            var photon = Library.CreateCard<ProbePhotonOverload>(IsUpgraded);

            yield return new AddCardsToHandAction(pylon,connon,ShieldBattery,photon);
        }
    }
}
